package com.shiro.service.impl;

import com.shiro.mapper.PermissionMapper;
import com.shiro.mapper.RolePermissionMapper;
import com.shiro.pojo.Permission;
import com.shiro.pojo.Role;
import com.shiro.service.PermissionService;
import com.shiro.service.RoleService;
import com.shiro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    RolePermissionMapper rolePermissionMapper;



    @Override
    public Set<String> listPermissions(String userName) {
        Set<String> result = new HashSet<>();

        List<Permission> permissions = permissionMapper.selectByUserName(userName);

        for (Permission permission : permissions) {
            result.add(permission.getName());
        }
        return result;
    }

    @Override
    public List<Permission> list() {
        List<Permission> permissions = permissionMapper.selectOrderById();
        return permissions;
    }

    @Override
    public void add(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKeySelective(permission);
    }

    @Override
    public List<Permission> list(Role role) {
        return permissionMapper.selectByRid(role.getId());
    }
}
